from odoo import api, fields, models, _
import logging
from odoo.exceptions import UserError

logger = logging.getLogger(__name__)


class Feedback_useful(models.Model):
    _name = 'feedback.useful'
    _description = 'user_info'

    name = fields.Char(
        '反馈表详情',
        default='反馈表详情'
    )
    vaccine_organization = fields.Many2one(
        'vaccine.organization',
        string='接种点',
        required=True
    )
    feedback_no = fields.Char(
        '反馈编号',
        index=True,
        readonly=True,
        default='自动生成'
    )

    department = fields.Selection([
        ('内科', '内科'),
        ('外科', '外科'),
        ('儿科', '儿科'),
        ('妇科', '妇科'),
        ('耳鼻喉科', '耳鼻喉科'),
    ], string='科室', defaut='内科', index=True)
    description = fields.Html(
        '过程描述',
        required=True
    )
    show = fields.Char(
        '表现'
    )
    result = fields.Selection([
        ('痊愈', '痊愈'),
        ('好转', '好转'),
        ('未好转', '未好转'),
        ('不详', '不详'),
        ('有后遗症', '有后遗症'),
        ('死亡', '死亡'),
    ], string='结果', defaut='痊愈', index=True)
    stop_recover = fields.Boolean(
        '停止注射后，反应/事件是否有减弱',
        default=True
    )
    status = fields.Selection([
        ('有效', '有效'),
        ('无效', '无效'),
        ('默认', '默认'),
    ], string='反馈状态', default='有效')
    reason = fields.Html(
        '有效原因',
        required=True
    )
    is_chat = fields.Boolean(
        '是否处于约谈状态',
        default=False
    )

    def check_chat(self):
        self.ensure_one()
        if self.is_chat:
            raise UserError(_('已被约谈!') % self.name)
        # 有则报错
        else:
            try:
                return {
                    'type': 'ir.actions.act_window',
                    'view_type': 'form',
                    'view_mode': 'calendar,tree,form',
                    'res_model': 'feedback.chat',
                    'target': 'new',
                    'context': {
                        'default_vaccine_organization': self.vaccine_organization.id
                    }
                }

            except Exception as e:
                raise UserError(_('错误：%s') % e)
